/**
 * Copyright 2013 Dennis Ippel
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.rajawali3d.materials.textures;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.opengl.Bitmap;


/**
 * Creates a texture from an animated GIF.
 * 
 * @author dennis.ippel
 *
 */
public class AnimatedGIFTexture extends ASingleTexture {
	private Canvas mCanvas;
    private PixelMap mMovie;
    private PixelMap mGIFBitmap;
	private int mResourceId;
	private int mWidth;
	private int mHeight;
	private int mTextureSize;
	private long mStartTime;
	private boolean mLoadNewGIF;
	
	public AnimatedGIFTexture(String name, int resourceId) {
		this(name, resourceId, 512);
	}
	
	/**
	 * Creates an animated GIF texture
	 * 
	 * @param resourceId	The animated GIF resource
	 * @param textureSize 	The power of two size
	 */
	public AnimatedGIFTexture(String name, int resourceId, int textureSize) {
		super(TextureType.DIFFUSE, name);
		mTextureSize = textureSize;
		mResourceId = resourceId;
		loadGIF();
	}
	
	public AnimatedGIFTexture(AnimatedGIFTexture other) {
		super(other);
		setFrom(other);
	}
	
	@Override
	public AnimatedGIFTexture clone() {
		return new AnimatedGIFTexture(this);
	}
	
	private void loadGIF() {
		Context context = TextureManager.getInstance().getContext();
        //mMovie = Movie.decodeStream(context.getResources().openRawResource(mResourceId));
        mMovie = Bitmap.getPixelMapFromResource(mResourceId, context);
        mWidth = Bitmap.getWidth(mMovie);
        mHeight = Bitmap.getHeight(mMovie);
		
        mGIFBitmap = Bitmap.create(mWidth, mHeight, PixelFormat.ARGB_8888);
        mCanvas = new Canvas(new Texture(mGIFBitmap));
        //mMovie.draw(mCanvas, 0, 0);
        mCanvas.drawPixelMapHolder(new PixelMapHolder(mMovie), 0, 0, new Paint());
		mBitmap = Bitmap.createScaledBitmap(mGIFBitmap, mTextureSize, mTextureSize, false);
	}
	
	/**
	 * Copies every property from another AnimatedGIFTexture object
	 * 
	 * @param other
	 *            another AnimatedGIFTexture object to copy from
	 */
	public void setFrom(AnimatedGIFTexture other)
	{
		super.setFrom(other);
		mBitmap = other.getBitmap();
		mCanvas = other.getCanvas();
		mMovie = other.getMovie();
		mWidth = other.getWidth();
		mHeight = other.getHeight();
		mTextureSize = other.getTextureSize();
	}
	
	public void rewind()
	{
        mStartTime = System.currentTimeMillis();
	}
	
	protected void replace() throws TextureException
	{
		if(mLoadNewGIF)
		{
			loadGIF();
			mLoadNewGIF = false;
		}
		super.replace();
	}

    public void update() throws TextureException {
//		if(mMovie == null || mMovie.duration() == 0) return;
//		long now = System.currentTimeMillis();
//		int relTime = (int)((now - mStartTime) % mMovie.duration());
//		mMovie.setTime(relTime);
//		mGIFBitmap.eraseColor(Color.TRANSPARENT);
//		mMovie.draw(mCanvas, 0, 0);
//		mBitmap = Bitmap.createScaledBitmap(mGIFBitmap, mTextureSize, mTextureSize, false);
        mCanvas.drawPixelMapHolder(new PixelMapHolder(mMovie), 0, 0, new Paint());
		mBitmap = Bitmap.createScaledBitmap(mGIFBitmap, mTextureSize, mTextureSize, false);
		TextureManager.getInstance().replaceTexture(this);
		replace();
	}
	
	public void setResourceId(int resourceId) {
		if(mResourceId == resourceId)
			return;
		mResourceId = resourceId;
		mLoadNewGIF = true;
	}
	
	protected void reset() throws TextureException
	{
		super.reset();
		
		if(mGIFBitmap != null)
		{
            mGIFBitmap.release();
			mGIFBitmap = null;
		}
		
		mCanvas = null;
		mMovie = null;
	}
	
	protected void remove() throws TextureException
	{
		if(mGIFBitmap != null)
		{
            mGIFBitmap.release();
			mGIFBitmap = null;
		}
		
		mCanvas = null;
		mMovie = null;
		
		super.remove();
	}

	public int getResourceId()
	{
		return mResourceId;
	}
	
	public Canvas getCanvas() {
		return mCanvas;
	}
	
    public PixelMap getMovie() {
		return mMovie;
	}
	
	public int getWidth() {
		return mWidth;
	}
	
	public int getHeight() {
		return mHeight;
	}
	
	public int getTextureSize() {
		return mTextureSize;
	}
}
